package com.example.hotel_lxy.ui.activity;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.text.Editable;
import android.text.LoginFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.example.hotel_lxy.R;
import com.example.hotel_lxy.application.ARouterPath;
import com.example.hotel_lxy.base.TypePay;
import com.example.hotel_lxy.bean.AddViewInfoBean;
import com.example.hotel_lxy.bean.BeanBean;
import com.example.hotel_lxy.bean.DialogDataReserveCbBean;
import com.example.hotel_lxy.bean.EventBean;
import com.example.hotel_lxy.bean.EventBusBean;
import com.example.hotel_lxy.bean.EventBusResereBean;
import com.example.hotel_lxy.bean.GuaranteeInfoBean;
import com.example.hotel_lxy.bean.ImgBean;
import com.example.hotel_lxy.bean.RvReserveBean;
import com.example.hotel_lxy.bean.RvStandardTypeBean;
import com.example.hotel_lxy.bean.ScheduledBean;
import com.example.hotel_lxy.bean.SpReserveBean;
import com.example.hotel_lxy.bean.ZeroSpSourceBean;
import com.example.hotel_lxy.net.Api;
import com.example.hotel_lxy.ui.dialog.DataGuaranteeDialog;
import com.example.hotel_lxy.ui.dialog.DataReserveAddViewDialog;
import com.example.hotel_lxy.ui.dialog.DataReserveCbDialog;
import com.example.hotel_lxy.ui.dialog.DataReserveDialog;
import com.example.hotel_lxy.ui.dialog.DataSelectionDialog;
import com.example.hotel_lxy.ui.dialog.DataSelectionDialogs;
import com.example.hotel_lxy.ui.dialog.ImageViewpagerDialog;
import com.example.hotel_lxy.ui.dialog.SendReserveDialog;
import com.example.hotel_lxy.ui.main.CalendarActivityTwo;
import com.example.hotel_lxy.utils.DatePhotoUtils;
import com.example.hotel_lxy.utils.GlideEngine;
import com.example.hotel_lxy.utils.JurisdictionUtils;
import com.example.hotel_lxy.utils.SPUtils;
import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.ysxsoft.common_base.base.BaseActivity;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.JsonUtils;
import com.ysxsoft.common_base.utils.ToastUtils;
import com.ysxsoft.common_base.view.custom.image.RoundImageView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.ysxsoft.common_base.base.BaseApplication.getContext;

/**
 * 预定
 */
@Route(path = "/main/ReserveActivity")
public class ReserveActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.rv_standard_type)
    RecyclerView rvStandardType;
    @BindView(R.id.line_is_meet)
    LinearLayout lineIsMeet;
    @BindView(R.id.text_tx)
    TextView textTx;
    @BindView(R.id.edit_tx)
    EditText editTx; //预订人
    @BindView(R.id.text_photo)
    EditText textPhoto;//预订人手机号
    @BindView(R.id.rg_sex)
    RadioGroup rgSex;
    @BindView(R.id.rb_male)
    RadioButton rbMale;
    @BindView(R.id.rb_female)
    RadioButton rbFemale;
    @BindView(R.id.text_time1)
    TextView textTime1;
    @BindView(R.id.rb_noon)
    RadioButton rbNoon;
    @BindView(R.id.rb_night)
    RadioButton rbNight;
    @BindView(R.id.radioGroup1)
    RadioGroup radioGroup1;
    @BindView(R.id.text_time2)
    TextView textTime2;
    @BindView(R.id.rb_noon2)
    RadioButton rbNoon2;
    @BindView(R.id.rb_night2)
    RadioButton rbNight2;
    @BindView(R.id.radioGroup2)
    RadioGroup radioGroup2;
    @BindView(R.id.edit_cb)
    EditText editCb;
    @BindView(R.id.text_Cb)
    TextView textCb;

    @BindView(R.id.line_add_view)
    LinearLayout lineAddView;
    @BindView(R.id.text_Add)
    TextView textAdd;
    @BindView(R.id.rv_hetong)
    RecyclerView rvHetong;
    @BindView(R.id.rv_qita)
    RecyclerView rvQita;
    @BindView(R.id.text1)
    TextView text1;

    @BindView(R.id.text_desk)
    EditText textDesk;
    @BindView(R.id.prepare_desk)
    EditText prepareDesk;
    @BindView(R.id.img_Cb)
    ImageView imgCb;
    @BindView(R.id.tv_source)
    TextView tvSource;
    @BindView(R.id.tv_view)
    TextView tvView;
    @BindView(R.id.text_service)
    TextView textService;

    @BindView(R.id.text_reserve)
    TextView textReserve;
    @BindView(R.id.text_message)
    TextView textMessage;
    @BindView(R.id.edit_content)
    EditText editContent;
    @BindView(R.id.rv_type)
    RecyclerView rvType;
    @BindView(R.id.desk)
    TextView desk;
    @BindView(R.id.text_prepare_desk)
    TextView textPrepareDesk;
    @BindView(R.id.text_caidan)
    TextView textCaidan;
    @BindView(R.id.line1)
    LinearLayout line1;

    private List<RvReserveBean> RvReserveList = new ArrayList<>();
    private int type_time;
    private TextView text_title;
    private List<TypePay> SpTypeList = new ArrayList<>();
    private List<TypePay> SpModeList = new ArrayList<>();
    private List<String> mlist_hetong = new ArrayList<>();
    private List<String> mlist_qita = new ArrayList<>();

    int img_type;
    List<ZeroSpSourceBean.DataBean.ListBean> list;
    TextView tv_type, tv_type_id;
    TextView tv_mode, tv_mode_id;


    private String director_id="";
    private String source_id="";
    //开始日期
    String begin_year = "";
    String begin_month = "";
    String begin_day = "";
    String begin_night = "1";//1中午2晚上
    //结束日期
    String end_year = "";
    String end_month = "";
    String end_day = "";
    String end_night = "1";//1中午2晚上
    //增值
    private String increase_money;//增值服务费
    private String increase = "";//增值服务id
    //性别
    private String sex = "1";//1男2女
    //类型id
    private String tid = "";
    //新郎新娘
    private String banquet_name = "";
    private List<BanquetNameBean> BanquetNamelsit = new ArrayList<>();//新郎新娘信息
    //餐标id
    private String cb_id = "";
    private String cb_did = "";
    private String d_money = "";

    //上传合同
    private String contract = "";
    //其他信息
    private String other = "";
    //是不是会议
    private String is_meeting = "1"; //1是2不是
    //确认方式
    private String confirm = "";
    private List<Object> mlsit = new ArrayList<>();//预收
    //    private List<GuaranteeBean> mlsitid = new ArrayList<>();//担保人
    private EditText money;
    private EditText number;
    private ImageView imgview;
    private TextView text_img;
    RadioButton rbYs;
    RadioButton rbDb;

    LinearLayout lineDbr;
    TextView edit_dbr;
    ImageView img_dbr;
    TextView edit_dbr_id;
    LinearLayout line_ys;
    TextView bh;
    private String editdbrid = "";
    private String editdbr = "";

    List<RvStandardTypeBean.DataBean.TypeBean> types;
    private int type;
    //区分预收款第几个布局
    private int img;
    private List<AddViewImg> addViewImgList = new ArrayList<>();
    String string;
    private int anInt = 0, anInj = 0;
    @Autowired
    String officeid;
    @Autowired
    String time;

    private boolean aBoolean=true;
    public static void start(String officeid, String time) {
        ARouter.getInstance().build(ARouterPath.getReserveActivity())
                .withString("officeid", officeid)
                .withString("time", time)
                .navigation();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reserve;
    }

    @Override
    public void doWork() {
        ARouter.getInstance().inject(this);
        super.doWork();
        SPUtils.put(ReserveActivity.this, "cid", "");
        string = SPUtils.get(ReserveActivity.this, "config", "").toString();

        String[] split = time.split("-");
        //开始日期
        begin_year = split[0];
        begin_month = split[1];
        begin_day = split[2];
        begin_night = split[3];
        if (begin_night.equals("null")) {
            begin_night = "1";
        }
        String[] split1 = time.split("-");

        //结束日期
        end_year = split1[0];
        end_month = split1[1];
        end_day = split1[2];
        end_night = split1[3];
        if (end_night.equals("null")) {
            end_night = "2";
        }
        if (begin_night.equals("2")) {
            rbNight.setChecked(true);
            rbNoon.setChecked(false);
        } else {
            rbNight.setChecked(false);
            rbNoon.setChecked(true);
        }
        if (end_night.equals("2")) {
            rbNight2.setChecked(true);
            rbNoon2.setChecked(false);
        } else {
            rbNight2.setChecked(false);
            rbNoon2.setChecked(true);
        }
        textTime1.setText("【日期】" + split[0] + "-" + split[1] + "-" + split[2]);
        textTime2.setText(" " + split1[0] + "-" + split1[1] + "-" + split1[2]);
        EventBus.getDefault().register(this);  //事件的注册
        showDataRvStandardType();

        textTx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                judgePermission();

            }
        });
        //性别
        rgSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_male) {//男
                    rbFemale.setChecked(false);
                    rbMale.setChecked(true);
                    sex = "1";
                } else if (checkedId == R.id.rb_female) {//女
                    rbFemale.setChecked(true);
                    rbMale.setChecked(false);
                    sex = "2";
                }

            }
        });

        //时间
        radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_noon) {//午
                    rbNight.setChecked(false);
                    rbNoon.setChecked(true);
                    begin_night = "1";
                } else if (checkedId == R.id.rb_night) {//晚
                    rbNight.setChecked(true);
                    rbNoon.setChecked(false);
                    begin_night = "2";
                }
            }
        });
        //会议
        radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_noon2) {//午
                    rbNight2.setChecked(false);
                    rbNoon2.setChecked(true);
                    end_night = "1";
                } else if (checkedId == R.id.rb_night2) {//晚
                    rbNight2.setChecked(true);
                    rbNoon2.setChecked(false);
                    end_night = "2";
                }
            }
        });

        editCb.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    editCb.setText("");
                    d_money = "";
                    if (!cb_did.equals("")) {
                        textCaidan.setText("已添加");
                        textCaidan.setTextColor(getResources().getColor(R.color.color_4d89ea));
                        textCaidan.setTextSize(15);
                        editCb.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                    }
                    aBoolean=true;
                } else {
                    if(editCb.getText().toString().equals("")){
                        editCb.setText(""+d_money);
                        textCaidan.setText("已添加");
                        textCaidan.setTextSize(15);
                        editCb.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                    }
                    if (!cb_did.equals("")) {
                        textCaidan.setText("已添加");
                        textCaidan.setTextColor(getResources().getColor(R.color.color_4d89ea));
                        textCaidan.setTextSize(15);
                        editCb.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        aBoolean=false;
                    }else{
                        textCaidan.setText("+");
                        textCaidan.setTextColor(getResources().getColor(R.color.color_333333));
                        textCaidan.setTextSize(20);
                        editCb.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                    }
                }
            }
        });
        editCb.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(aBoolean){
                    d_money=s.toString();
                    cb_id = "";
                    cb_did = "";
                }
            }
        });
        showDataSpTypeMode();
        showRVHtImg();
        showRVQTImg();

        textDesk.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals("")) {
                    float i = Float.valueOf(s.toString());
                    Log.e("TAG", is_meeting + "  123");
                    if (!is_meeting.equals("1")) {
                        float max = Float.valueOf(SPUtils.get(ReserveActivity.this, "max", "").toString());
                        if (i > max) {
                            String sq= max+"".replace(".0","");
                            ToastUtils.show(ReserveActivity.this, "桌数不能大于桌数" + sq.replace(".0","") + "哦");
                        }
                    }

                }
            }
        });
        showData();
        editContent.setOnTouchListener(new SolveEditTextScrollClash(editContent));

        line1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editContent.setFocusable(true);
                editContent.setFocusableInTouchMode(true);
                editContent.requestFocus();
                InputMethodManager imm = (InputMethodManager)ReserveActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

            }
        });
    }

    //其他图片
    private void showRVQTImg() {
        if(mlist_qita.size()<20){
            mlist_qita.add(0, "");
        }
        rvQita.setLayoutManager(new GridLayoutManager(ReserveActivity.this, 3));
        RBaseAdapter<String> rBaseAdapter = new RBaseAdapter<String>(ReserveActivity.this, R.layout.item_returnvist_img, mlist_qita) {
            @Override
            protected void fillItem(RViewHolder holder, String item, int position) {
                RoundImageView img = holder.itemView.findViewById(R.id.img_round);
                ImageView img_detele = holder.itemView.findViewById(R.id.img_detele);
                if (!item.equals("")) {
                    img_detele.setVisibility(View.VISIBLE);
                    Glide.with(ReserveActivity.this).load(item).into(img);
                } else {
                    img_detele.setVisibility(View.GONE);
                    Glide.with(ReserveActivity.this).load(R.mipmap.icon_rv_img).into(img);
                }
                img_detele.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mlist_qita.remove(position);
                        if (mlist_qita.size() < 20) {
                            if (!mlist_qita.contains("")) {
                                mlist_qita.add(0, "");
                            }
                        }
                        notifyDataSetChanged();
                    }
                });
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.equals("")) {
                            img_type = 2;
                            showImg();
                        } else {
                            if(mlist_qita.size()!=20){
                                ImageViewpagerDialog.show(ReserveActivity.this, null, mlist_qita, position-1);
                            }else{
                                ImageViewpagerDialog.show(ReserveActivity.this, null, mlist_qita, position);
                            }

                        }
                    }
                });
            }

            @Override
            protected int getViewType(String item, int position) {
                return 0;
            }
        };
        rvQita.setAdapter(rBaseAdapter);
    }

    //合同图片
    private void showRVHtImg() {
        if(mlist_hetong.size()<20){
            mlist_hetong.add(0, "");
        }
        rvHetong.setLayoutManager(new GridLayoutManager(ReserveActivity.this, 3));
        RBaseAdapter<String> rBaseAdapter = new RBaseAdapter<String>(ReserveActivity.this, R.layout.item_returnvist_img, mlist_hetong) {
            @Override
            protected void fillItem(RViewHolder holder, String item, int position) {
                RoundImageView img = holder.itemView.findViewById(R.id.img_round);
                ImageView img_detele = holder.itemView.findViewById(R.id.img_detele);
                if (!item.equals("")) {
                    Log.e("TAG", item);
                    img_detele.setVisibility(View.VISIBLE);
                    Glide.with(ReserveActivity.this).load(item).into(img);
                } else {
                    img_detele.setVisibility(View.GONE);
                    Glide.with(ReserveActivity.this).load(R.mipmap.icon_rv_img).into(img);
                }
                img_detele.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mlist_hetong.remove(position);
                        if (mlist_hetong.size() < 20) {
                            if (!mlist_hetong.contains("")) {
                                mlist_hetong.add(0, "");
                            }
                        }
                        notifyDataSetChanged();
                    }
                });
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (item.equals("")) {
                            img_type = 1;
                            showImg();
                        } else {
                            if(mlist_hetong.size()!=20){
                                ImageViewpagerDialog.show(ReserveActivity.this, null, mlist_hetong, position-1);
                            }else{
                                ImageViewpagerDialog.show(ReserveActivity.this, null, mlist_hetong, position);
                            }
                            //ImageViewpagerDialog.show(ReserveActivity.this, null, mlist_hetong, position);
                        }
                    }
                });
            }

            @Override
            protected int getViewType(String item, int position) {
                return 0;
            }
        };
        rvHetong.setAdapter(rBaseAdapter);
    }

    //图片选择器
    private void showImg() {
        if (img_type == 2) {//其他
            if(mlist_qita.get(0).equals("")){
                mlist_qita.remove(0);
            }

            int j=20-Integer.valueOf(mlist_qita.size());
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .loadImageEngine(GlideEngine.createGlideEngine())
                    .selectionMode(100)
                    .maxSelectNum(j)
                    .previewImage(true)
                    .compress(true)//图片是否压缩
                    .minimumCompressSize(70)// 小于100kb的图片不压缩
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        } else if (img_type == 1) {
            if(mlist_hetong.get(0).equals("")){
                mlist_hetong.remove(0);
            }
            int i=20-Integer.valueOf(mlist_hetong.size());
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .loadImageEngine(GlideEngine.createGlideEngine())
                    .selectionMode(100)
                    .maxSelectNum(i)
                    .previewImage(true)
                    .compress(true)//图片是否压缩
                    .minimumCompressSize(70)// 小于100kb的图片不压缩
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }

    }

    //预收款的类型与方式
    private void showDataSpTypeMode() {
        OkHttpUtils.post()
                .url(Api.POST_PAYMENT)
                .addHeader("flag", SPUtils.get(ReserveActivity.this, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "预收款的类型与方式onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "预收款的类型与方式onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(ReserveActivity.this, "token", "");
                            Intent intent = new Intent(ReserveActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        SpReserveBean spReserveBean = JsonUtils.parseByGson(response, SpReserveBean.class);
                        if (beanBean.getCode().equals("0")) {
                            for (int i = 0; i < spReserveBean.getData().getType().size(); i++) {
                                TypePay typePay = new TypePay();
                                typePay.setId(spReserveBean.getData().getType().get(i).getId() + "");
                                typePay.setName(spReserveBean.getData().getType().get(i).getName());
                                SpTypeList.add(typePay);
                            }
                            for (int i = 0; i < spReserveBean.getData().getPay().size(); i++) {
                                TypePay typePay = new TypePay();
                                typePay.setId(spReserveBean.getData().getPay().get(i).getId() + "");
                                typePay.setName(spReserveBean.getData().getPay().get(i).getName());
                                SpModeList.add(typePay);
                            }
                        }
                    }
                });
    }

    //获取宴会厅的类型
    private void showDataRvStandardType() {
//        SPUtils.get(ReserveActivity.this, "name", "").toString()''
        OkHttpUtils.post()
                .url(Api.POST_INDEX_SCHEDULED)
                .addHeader("flag", SPUtils.get(ReserveActivity.this, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "宴会厅onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "宴会厅onResponse   " + response);
                        if (response.equals("")) {
                            return;
                        }
                        BeanBean beanBean = JsonUtils.parseObject(response, BeanBean.class);
                        if (beanBean.getCode().equals("0")) {
                            ScheduledBean scheduledBean = JsonUtils.parseByGson(response, ScheduledBean.class);
                            for (int i = 0; i < scheduledBean.getData().size(); i++) {
                                if (officeid.equals(scheduledBean.getData().get(i).getId() + "")) {
                                    text1.setText("" + scheduledBean.getData().get(i).getName());
                                    SPUtils.put(ReserveActivity.this, "sched_id", scheduledBean.getData().get(i).getId() + "");
                                    SPUtils.put(ReserveActivity.this, "max", scheduledBean.getData().get(i).getMax() + "");

                                    d_money=scheduledBean.getData().get(i).getMoney()+"";
                                    return;
                                }
                            }
                        } else if (beanBean.getCode().equals("4")) {
                            Intent intent = new Intent(getContext(), LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    }
                });


        RvReserveList.clear();
        OkHttpUtils.post()
                .url(Api.POST_STANDARD_TYPE)
                .addHeader("flag", SPUtils.get(ReserveActivity.this, "token", "").toString())
                .addParams("bid", "" + officeid)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "获取宴会厅的类型onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "获取宴会厅的类型onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(ReserveActivity.this, "token", "");
                            Intent intent = new Intent(ReserveActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        RvStandardTypeBean rvStandardTypeBean = JsonUtils.parseByGson(response, RvStandardTypeBean.class);
                        if (beanBean.getCode().equals("0")) {
                            types = rvStandardTypeBean.getData().getType();
                            if(types.size()!=0){
                                types.get(0).setaBoolean(true);
                            }
                            rvStandardType.setLayoutManager(new GridLayoutManager(ReserveActivity.this, 3));
                            RBaseAdapter<RvStandardTypeBean.DataBean.TypeBean> rvReserveBeanRBaseAdapter = new RBaseAdapter<RvStandardTypeBean.DataBean.TypeBean>(
                                    ReserveActivity.this, R.layout.item_reserve_activity_rv,
                                    types) {
                                @Override
                                protected void fillItem(RViewHolder holder, RvStandardTypeBean.DataBean.TypeBean item, int position) {
                                    CheckBox checkbox = holder.itemView.findViewById(R.id.checkbox);
                                    checkbox.setChecked(item.isaBoolean());
                                    if (item.isaBoolean()) {
                                        SPUtils.put(ReserveActivity.this, "money", item.getMoney() + "");
                                        tid = item.getId() + "";
                                        if (item.getIs_meet() == 1) {
                                            is_meeting = "1";
                                            textTime2.setText(""+textTime1.getText().toString().replace("【日期】",""));
                                            String[] split = textTime1.getText().toString().replace("【日期】","").split("-");
                                            end_year = split[0];
                                            end_month = split[1];
                                            end_day = split[2];
                                            lineIsMeet.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    lineIsMeet.setVisibility(View.VISIBLE);
                                                }
                                            });
                                            rbNoon.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    rbNoon.setText("上午");
                                                }
                                            });
                                            rbNoon2.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    rbNoon2.setText("上午");
                                                }
                                            });
                                            rbNight.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    rbNight.setText("下午");
                                                }
                                            });
                                            rbNight2.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    rbNight2.setText("下午");
                                                }
                                            });
//                                            rbNoon.setText("上午");
//                                            rbNoon2.setText("上午");
//                                            rbNight.setText("下午");
//                                            rbNight2.setText("下午");
                                            desk.setText("【 人 数 】");
                                            textPrepareDesk.setVisibility(View.GONE);
                                            prepareDesk.setVisibility(View.GONE);
                                        } else if (item.getIs_meet() == 2) {
                                            rbNoon.setText("中午");
                                            rbNight.setText("晚上");
                                            is_meeting = "2";
                                            lineIsMeet.setVisibility(View.GONE);
                                            desk.setText("【 桌 数 】");
                                            textPrepareDesk.setVisibility(View.VISIBLE);
                                            prepareDesk.setVisibility(View.VISIBLE);
                                        }
                                        BanquetNamelsit.clear();
                                        for (int i = 0; i < item.getValue().size(); i++) {
                                            BanquetNameBean banquetNameBean = new BanquetNameBean();
                                            banquetNameBean.setBid("");
                                            banquetNameBean.setName("");
                                            banquetNameBean.setValue("");
                                            BanquetNamelsit.add(banquetNameBean);
                                        }
                                        rvType.setLayoutManager(new LinearLayoutManager(ReserveActivity.this));
                                        RBaseAdapter<RvStandardTypeBean.DataBean.TypeBean.ValueBean> valueBeanRBaseAdapter = new RBaseAdapter<RvStandardTypeBean.DataBean.TypeBean.ValueBean>
                                                (ReserveActivity.this,
                                                        R.layout.item_reserve_rv_type, item.getValue()) {
                                            @Override
                                            protected void fillItem(RViewHolder holder, RvStandardTypeBean.DataBean.TypeBean.ValueBean item, int position) {
                                                TextView name = holder.itemView.findViewById(R.id.name);
                                                TextView names = holder.itemView.findViewById(R.id.names);
                                                EditText groomname = holder.itemView.findViewById(R.id.groomname);
                                                EditText groomphoto = holder.itemView.findViewById(R.id.groomphoto);
                                                name.setText("" + item.getName());
                                                names.setText("" + item.getNames());
                                                groomname.addTextChangedListener(new TextWatcher() {
                                                    @Override
                                                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                                    }

                                                    @Override
                                                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                                                    }

                                                    @Override
                                                    public void afterTextChanged(Editable s) {

                                                        BanquetNamelsit.get(position).setBid(item.getId() + "");
                                                        BanquetNamelsit.get(position).setName(groomname.getText().toString() + "");

                                                    }
                                                });
                                                groomphoto.addTextChangedListener(new TextWatcher() {
                                                    @Override
                                                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                                    }

                                                    @Override
                                                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                                                    }

                                                    @Override
                                                    public void afterTextChanged(Editable s) {

                                                        BanquetNamelsit.get(position).setBid(item.getId() + "");
                                                        BanquetNamelsit.get(position).setValue(groomphoto.getText().toString() + "");

                                                    }
                                                });

                                            }

                                            @Override
                                            protected int getViewType(RvStandardTypeBean.DataBean.TypeBean.ValueBean item, int position) {
                                                return 0;
                                            }
                                        };
                                        rvType.setAdapter(valueBeanRBaseAdapter);
                                    } else {
                                        rbNoon.setText("中午");
                                        rbNight.setText("晚上");
                                        lineIsMeet.setVisibility(View.GONE);
                                    }

                                    checkbox.setText(item.getName());
                                    checkbox.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            for (RvStandardTypeBean.DataBean.TypeBean data : types) {
                                                data.setaBoolean(false);
                                            }
                                            //tid = item.getId() + "";
                                            types.get(position).setaBoolean(true);

                                            notifyDataSetChanged();
                                        }
                                    });
                                }

                                @Override
                                protected int getViewType(RvStandardTypeBean.DataBean.TypeBean item, int position) {
                                    return 0;
                                }
                            };
                            rvStandardType.setAdapter(rvReserveBeanRBaseAdapter);
                        }
                    }
                });
    }

    //获取宴会厅类型
    private void officetype() {
        RvReserveList.clear();
        OkHttpUtils.post()
                .url(Api.POST_STANDARD_TYPE)
                .addHeader("flag", SPUtils.get(ReserveActivity.this, "token", "").toString())
                .addParams("bid", "" + officeid)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "获取宴会厅的类型onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "获取宴会厅的类型onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(ReserveActivity.this, "token", "");
                            Intent intent = new Intent(ReserveActivity.this, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        RvStandardTypeBean rvStandardTypeBean = JsonUtils.parseByGson(response, RvStandardTypeBean.class);
                        if (beanBean.getCode().equals("0")) {
                            types = rvStandardTypeBean.getData().getType();
                            rvStandardType.setLayoutManager(new GridLayoutManager(ReserveActivity.this, 3));
                            RBaseAdapter<RvStandardTypeBean.DataBean.TypeBean> rvReserveBeanRBaseAdapter = new RBaseAdapter<RvStandardTypeBean.DataBean.TypeBean>(
                                    ReserveActivity.this, R.layout.item_reserve_activity_rv,
                                    types) {
                                @Override
                                protected void fillItem(RViewHolder holder, RvStandardTypeBean.DataBean.TypeBean item, int position) {
                                    CheckBox checkbox = holder.itemView.findViewById(R.id.checkbox);
                                    checkbox.setChecked(item.isaBoolean());
                                    if (item.isaBoolean()) {
                                        SPUtils.put(ReserveActivity.this, "money", item.getMoney() + "");
                                        if (item.getIs_meet() == 1) {
                                            is_meeting = "1";
                                            lineIsMeet.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    lineIsMeet.setVisibility(View.VISIBLE);
                                                }
                                            });
                                            rbNoon.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    rbNoon.setText("上午");
                                                }
                                            });
                                            rbNoon2.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    rbNoon2.setText("上午");
                                                }
                                            });
                                            rbNight.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    rbNight.setText("下午");
                                                }
                                            });
                                            rbNight2.post(new Runnable() {
                                                @Override
                                                public void run() {
                                                    rbNight2.setText("下午");
                                                }
                                            });
//                                            rbNoon.setText("上午");
//                                            rbNoon2.setText("上午");
//                                            rbNight.setText("下午");
//                                            rbNight2.setText("下午");
                                            desk.setText("【 人 数 】");
                                            textPrepareDesk.setVisibility(View.GONE);
                                            prepareDesk.setVisibility(View.GONE);
                                        } else if (item.getIs_meet() == 2) {
                                            rbNoon.setText("中午");
                                            rbNight.setText("晚上");
                                            is_meeting = "2";
                                            lineIsMeet.setVisibility(View.GONE);
                                            desk.setText("【 桌 数 】");
                                            textPrepareDesk.setVisibility(View.VISIBLE);
                                            prepareDesk.setVisibility(View.VISIBLE);
                                        }
                                        BanquetNamelsit.clear();
                                        for (int i = 0; i < item.getValue().size(); i++) {
                                            BanquetNameBean banquetNameBean = new BanquetNameBean();
                                            banquetNameBean.setBid("");
                                            banquetNameBean.setName("");
                                            banquetNameBean.setValue("");
                                            BanquetNamelsit.add(banquetNameBean);
                                        }
                                        rvType.setLayoutManager(new LinearLayoutManager(ReserveActivity.this));
                                        RBaseAdapter<RvStandardTypeBean.DataBean.TypeBean.ValueBean> valueBeanRBaseAdapter = new RBaseAdapter<RvStandardTypeBean.DataBean.TypeBean.ValueBean>
                                                (ReserveActivity.this,
                                                        R.layout.item_reserve_rv_type, item.getValue()) {
                                            @Override
                                            protected void fillItem(RViewHolder holder, RvStandardTypeBean.DataBean.TypeBean.ValueBean item, int position) {
                                                TextView name = holder.itemView.findViewById(R.id.name);
                                                TextView names = holder.itemView.findViewById(R.id.names);
                                                EditText groomname = holder.itemView.findViewById(R.id.groomname);
                                                EditText groomphoto = holder.itemView.findViewById(R.id.groomphoto);
                                                name.setText("" + item.getName());
                                                names.setText("" + item.getNames());
                                                groomname.addTextChangedListener(new TextWatcher() {
                                                    @Override
                                                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                                    }

                                                    @Override
                                                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                                                    }

                                                    @Override
                                                    public void afterTextChanged(Editable s) {

                                                        BanquetNamelsit.get(position).setBid(item.getId() + "");
                                                        BanquetNamelsit.get(position).setName(groomname.getText().toString() + "");

                                                    }
                                                });
                                                groomphoto.addTextChangedListener(new TextWatcher() {
                                                    @Override
                                                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                                    }

                                                    @Override
                                                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                                                    }

                                                    @Override
                                                    public void afterTextChanged(Editable s) {

                                                        BanquetNamelsit.get(position).setBid(item.getId() + "");
                                                        BanquetNamelsit.get(position).setValue(groomphoto.getText().toString() + "");

                                                    }
                                                });

                                            }

                                            @Override
                                            protected int getViewType(RvStandardTypeBean.DataBean.TypeBean.ValueBean item, int position) {
                                                return 0;
                                            }
                                        };
                                        rvType.setAdapter(valueBeanRBaseAdapter);
                                    } else {
                                        rbNoon.setText("中午");
                                        rbNight.setText("晚上");
                                        lineIsMeet.setVisibility(View.GONE);
                                    }

                                    checkbox.setText(item.getName());
                                    checkbox.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v) {
                                            for (RvStandardTypeBean.DataBean.TypeBean data : types) {
                                                data.setaBoolean(false);
                                            }
                                            tid = item.getId() + "";
                                            types.get(position).setaBoolean(true);

                                            notifyDataSetChanged();
                                        }
                                    });
                                }

                                @Override
                                protected int getViewType(RvStandardTypeBean.DataBean.TypeBean item, int position) {
                                    return 0;
                                }
                            };
                            rvStandardType.setAdapter(rvReserveBeanRBaseAdapter);
                        }
                    }
                });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }



    @OnClick({R.id.text_time1, R.id.text_time2, R.id.back, R.id.text_Add, R.id.text1, R.id.img_Cb, R.id.tv_view, R.id.tv_source
            , R.id.text_service, R.id.text_reserve, R.id.text_message, R.id.text_caidan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_time1://开始日历
                String[] splitq = time.split("-");
                String s = splitq[0] + "-" + splitq[1];
                type_time = 1;
                if(!splitq[1].equals("10")){
                    CalendarActivityResere.start(Integer.valueOf(splitq[0]),Integer.valueOf(splitq[1].replace("0",""))-1,1);
                }else{
                    CalendarActivityResere.start(Integer.valueOf(splitq[0]),Integer.valueOf(splitq[1])-1,1);
                }
                break;
            case R.id.text_time2://会议结束日历
                String[] splits = textTime2.getText().toString().split("-");
                type_time = 2;
                if(!splits[1].equals("10")){
                    CalendarActivityResere.start(Integer.valueOf(splits[0]),Integer.valueOf(splits[1].replace("0",""))-1,1);
                }else{
                    CalendarActivityResere.start(Integer.valueOf(splits[0]),Integer.valueOf(splits[1])-1,1);
                }
                break;
            case R.id.back://返回
                finish();
                break;
            case R.id.text_Add://添加预收款
              //  ToastUtils.shortToast(ReserveActivity.this,"1开启  2否  "+string);
                int childCount = lineAddView.getChildCount();
                if (childCount != 0) {
                    boolean addviewinfo = addviewinfo();
                    if (addviewinfo) {
                        return;
                    }
                }
                View views = LinearLayout.inflate(ReserveActivity.this, R.layout.add_reserve_view, null);
                views.setTag(lineAddView.getChildCount());//设置标记
                initFind(views, lineAddView.getChildCount());
                views.setPadding(0, 10, 0, 0);
                lineAddView.addView(views);
                if (!string.equals("1")) {
                    textAdd.setEnabled(false);
                }
                if(!JurisdictionUtils.getReceiptStatus(ReserveActivity.this)){
                    imgview.setVisibility(View.INVISIBLE);
                    number.setVisibility(View.INVISIBLE);
                    bh.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.text1://宴会厅
                DataReserveDialog.show(ReserveActivity.this, null).setListener(new DataReserveDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String id, String name, String max) {
                        text1.setText("" + name);
                        SPUtils.put(ReserveActivity.this, "name", name);
                        SPUtils.put(ReserveActivity.this, "sched_id", id);
                        SPUtils.put(ReserveActivity.this, "max", max);
                        officeid=id;
                        officetype();
                    }
                });
                break;
            case R.id.img_Cb://选择餐标
                editCb.setFocusable(true);
                editCb.clearFocus();
                DataReserveCbDialog.show(ReserveActivity.this, null,tid).setListener(new DataReserveCbDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String id, String did, String name, String d_moneys) {
                        if(!name.equals("")){
                            editCb.setText("" + name);
                            cb_id = id;
                            cb_did = did;
                            d_money = d_moneys;
                            textCaidan.setText("已添加");
                            textCaidan.setTextColor(getResources().getColor(R.color.color_4d89ea));
                            textCaidan.setTextSize(15);
                        }
                    }
                });
                break;
            case R.id.text_caidan:
                if (editCb.getText().toString().equals("")) {
                    ToastUtils.show(ReserveActivity.this, "请先输入餐标");
                    return;
                }
                MealLabelActivity.start(cb_did, cb_id,tid);
                break;

            case R.id.tv_view://客户经理
                boolean szkehujl = JurisdictionUtils.getSzkehujl(ReserveActivity.this);
                if (!szkehujl) {
                    ToastUtils.longToast(ReserveActivity.this, "当前账号没有操作权限哦");
                    return;
                }
                DataSelectionDialogs.show(ReserveActivity.this, null, Api.POST_ZERO_VIEW).setListener(new DataSelectionDialogs.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String id, String name) {
                        director_id = id + "";
                        tvView.setText("" + name);
                    }
                });
                break;
            case R.id.tv_source://客户来源
                DataSelectionDialog.show(ReserveActivity.this, null, Api.POST_SCHEDULED_SOURCE).setListener(new DataSelectionDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String id, String name) {
                        source_id = id + "";
                        tvSource.setText("" + name);
                    }
                });
                break;
            case R.id.text_service:
                IncrementServiceActivity.start(increase);
                break;
            case R.id.text_reserve://预定
                if (tid.equals("")) {
                    ToastUtils.show(ReserveActivity.this, "请先选择类型");
                    return;
                }
                if (editTx.getText().toString().equals("")) {
                    ToastUtils.show(ReserveActivity.this, "预订人不能为空");
                    return;
                }
                if (textPhoto.getText().toString().equals("")) {
                    ToastUtils.show(ReserveActivity.this, "预订人手机号不能为空");
                    return;
                }
                if (textPhoto.getText().toString().length()!=11) {
                    ToastUtils.show(ReserveActivity.this, "请输入正确的手机号码");
                    return;
                }
                if (source_id.equals("")) {
                    ToastUtils.show(ReserveActivity.this, "请先选择客户来源");
                    return;
                }
                if (director_id.equals("")) {
                    ToastUtils.show(ReserveActivity.this, "请先选择客户经理");
                    return;
                }
                if (textDesk.getText().toString().equals("")) {
                    if (!is_meeting.equals("1")) {
                        ToastUtils.show(ReserveActivity.this, "桌数不能为空");
                    } else {
                        ToastUtils.show(ReserveActivity.this, "人数不能为空");
                    }
                    return;
                }
                if(!DatePhotoUtils.isPhone(textPhoto.getText().toString())){
                    ToastUtils.show(ReserveActivity.this, "请输入正确的手机号码");
                    return;
                }
//                if(editCb.getText().toString().equals("")){
//                    ToastUtils.show(ReserveActivity.this, "餐标不能为空");
//                    return;
//                }
                if(aBoolean){
                    String string = SPUtils.get(mContext, "money", "").toString();
                    if(!string.equals("")){
                        if(!d_money.equals("")){
                            if (Float.valueOf(string) > Float.valueOf(d_money)) {
                                editCb.setText(""+string);
                                ToastUtils.shortToast(mContext,"不能低于最低餐标"+string+"元");
                                return;
                            }
                        }
                    }
                }

                int i = Integer.valueOf(textDesk.getText().toString());

                if (!is_meeting.equals("1")) {
                    int max = Integer.valueOf(SPUtils.get(ReserveActivity.this, "max", "").toString());
                    if (i > max) {
                        ToastUtils.show(ReserveActivity.this, "桌数不能大于桌数" + max + "哦");
                        return;
                    }
                }

                String data = null;
                String strings;
                String endString;
                String[] split = textTime1.getText().toString().replace("【日期】", "").split("-");
                String[] split2 = textTime2.getText().toString().split("-");
                if (is_meeting.equals("1")) {
                    if (begin_night.equals("1")) {
                        strings = "上午";
                    } else {
                        strings = "下午";
                    }
                    if (end_night.equals("1")) {
                        endString = "上午";
                    } else {
                        endString = "下午";
                    }
                    data = "确定要预订" + split[0] + "年" + split[1] + "月" + split[2] + "日" + strings + "至" + split2[0] + "年" + split2[1] + "月" + split2[2] + "日" + endString + "  " +
                            text1.getText().toString() + "吗？";
                } else {
                    if (begin_night.equals("1")) {
                        strings = "中午";
                    } else {
                        strings = "晚上";
                    }
                    data = "确定要预订" + split[0] + "年" + split[1] + "月" + split[2] + "日" + strings + "  " +
                            text1.getText().toString() + "吗？";
                }

                SendReserveDialog.show(ReserveActivity.this, null, data).setListener(new SendReserveDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String s) {
                        if (s.equals("1")) {
                            String sched_id = SPUtils.get(ReserveActivity.this, "sched_id", "").toString();
                            String[] split = textTime1.getText().toString().replace("【日期】", "").split("-");
                            SPUtils.put(ReserveActivity.this, "currentDate", split[0] + "-" + split[1] + "-" + split[2]);
                            SPUtils.put(ReserveActivity.this, "homeorder", split[0] + "-" + split[1] + "-" + split[2] + "-" + begin_night + "-" + sched_id);
                            type = 1;
                            showFileImg();
                        }
                    }
                });
                break;
            case R.id.text_message://发短信
                if (tid.equals("")) {
                    ToastUtils.show(ReserveActivity.this, "请先选择类型");
                    return;
                }
                if (editTx.getText().toString().equals("")) {
                    ToastUtils.show(ReserveActivity.this, "预订人不能为空");
                    return;
                }
                if (textPhoto.getText().toString().equals("")) {
                    ToastUtils.show(ReserveActivity.this, "预订人手机号不能为空");
                    return;
                }
                if (textPhoto.getText().toString().length()!=11) {
                    ToastUtils.show(ReserveActivity.this, "请输入正确的手机号码");
                    return;
                }
                if (source_id.equals("")) {
                    ToastUtils.show(ReserveActivity.this, "请先选择客户来源");
                    return;
                }
                if (director_id.equals("")) {
                    ToastUtils.show(ReserveActivity.this, "请先选择客户经理");
                    return;
                }
                if (textDesk.getText().toString().equals("")) {
                    if (!is_meeting.equals("1")) {
                        ToastUtils.show(ReserveActivity.this, "桌数不能为空");
                    } else {
                        ToastUtils.show(ReserveActivity.this, "人数不能为空");
                    }
                    return;
                }
                if(!DatePhotoUtils.isPhone(textPhoto.getText().toString())){
                    ToastUtils.show(ReserveActivity.this, "请输入正确的手机号码");
                    return;
                }
//                if(editCb.getText().toString().equals("")){
//                    ToastUtils.show(ReserveActivity.this, "餐标不能为空不能为空");
//                    return;
//                }
                if(aBoolean){
                    String strin = SPUtils.get(mContext, "money", "").toString();
                    if(!strin.equals("")){
                        if (Float.valueOf(strin) > Float.valueOf(d_money)) {
                            editCb.setText(""+strin);
                            ToastUtils.shortToast(mContext,"不能低于最低餐标"+strin+"元");
                            return;
                        }
                    }
                }


                int i1 = Integer.valueOf(textDesk.getText().toString());

                if (!is_meeting.equals("1")) {
                    int max = Integer.valueOf(SPUtils.get(ReserveActivity.this, "max", "").toString());
                    if (i1 > max) {
                        ToastUtils.show(ReserveActivity.this, "桌数不能大于桌数" + max + "哦");
                        return;
                    }
                }
                String datames = null;
                String stringsmes;
                String endStringmes;
                String[] splitmes = textTime1.getText().toString().replace("【日期】", "").split("-");
                String[] split2mes = textTime2.getText().toString().split("-");
                if (is_meeting.equals("1")) {
                    if (begin_night.equals("1")) {
                        stringsmes = "上午";
                    } else {
                        stringsmes = "下午";
                    }
                    if (end_night.equals("1")) {
                        endStringmes = "上午";
                    } else {
                        endStringmes = "下午";
                    }
                    datames = "确定要预订" + splitmes[0] + "年" + splitmes[1] + "月" + splitmes[2] + "日" + stringsmes + "至" + split2mes[0] + "年" + split2mes[1] + "月" + split2mes[2] + "日" + endStringmes + "  " +
                            text1.getText().toString() + "，并发送预订信息给客户吗？";
                } else {
                    if (begin_night.equals("1")) {
                        stringsmes = "中午";
                    } else {
                        stringsmes = "晚上";
                    }
                    datames = "确定要预订" + splitmes[0] + "年" + splitmes[1] + "月" + splitmes[2] + "日" + stringsmes + "  " +
                            text1.getText().toString() + "，并发送预订信息给客户吗？";
                }
                SendReserveDialog.show(ReserveActivity.this, null, datames).setListener(new SendReserveDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String s) {
                        if (s.equals("1")) {
                            String sched_id = SPUtils.get(ReserveActivity.this, "sched_id", "").toString();
                            String[] split = textTime1.getText().toString().replace("【日期】", "").split("-");
                            SPUtils.put(ReserveActivity.this, "currentDate", split[0] + "-" + split[1] + "-" + split[2]);
                            SPUtils.put(ReserveActivity.this, "homeorder", split[0] + "-" + split[1] + "-" + split[2] + "-" + begin_night + "-" + sched_id);
                            Log.e("TAG", SPUtils.get(ReserveActivity.this, "currentDate", split[0] + "-" + split[1] + "-" + split[2]).toString());
                            type = 2;
                            showFileImg();
                        }
                    }
                });
                break;
        }
    }

    //确认方式添加判断
    private boolean addviewinfo() {
        for (int i = 0; i < lineAddView.getChildCount(); i++) {
            View childAt = lineAddView.getChildAt(i);
            initFind(childAt, i);
            if (rbYs.isChecked()) {
                if (money.getText().toString().equals("")) {
                    ToastUtils.shortToast(ReserveActivity.this, "请先完善确认方式");
                    return true;
                }
                if(JurisdictionUtils.getReceiptStatus(ReserveActivity.this)){
                    if (number.getText().toString().equals("")) {
                        ToastUtils.shortToast(ReserveActivity.this, "请填写收据编号");
                        return true;
                    }
                }

            } else if (rbDb.isChecked()) {
                if (edit_dbr_id.getText().toString().equals("")) {
                    ToastUtils.shortToast(ReserveActivity.this, "请先完善确认方式");
                    return true;
                }
            }
        }
        return false;
    }

    // 区分确认方式
    private void showDataAddReserve() {
        mlsit.clear();
        for (int i = 0; i < lineAddView.getChildCount(); i++) {
            int childCount = lineAddView.getChildCount();
            if (childCount != 0) {
//                boolean addviewinfo = addviewinfo();
//                if (addviewinfo) {
//                    return;
//                } else {
                    AddViewInfoBean addViewInfoBean = new AddViewInfoBean();
                    View childAt = lineAddView.getChildAt(i);
                    initFind(childAt, i);
                    if (rbYs.isChecked()) {
                        if(!money.getText().toString().equals("")){
                            BigDecimal a = new BigDecimal(money.getText().toString());
                            BigDecimal b = new BigDecimal("0");
                            int val2 = a.compareTo(b);
                            if(val2==1){
                                AddViewInfoBean.AddViewInfo addViewInfoList = addViewInfoBean.getAddViewInfoList();
                                addViewInfoList.setMoney(money.getText().toString());
                                if(!tv_type_id.getText().toString().equals("")){
                                    addViewInfoList.setType_id(tv_type_id.getText().toString());
                                }else{
                                    ToastUtils.shortToast(ReserveActivity.this,"请填写预付款支付类型");
                                    mlsit.clear();
                                    return;
                                }
                                if(!tv_mode_id.getText().toString().equals("")){
                                    addViewInfoList.setPay_type(tv_mode_id.getText().toString());
                                }else{
                                    ToastUtils.shortToast(ReserveActivity.this,"请填写预付款支付方式");
                                    mlsit.clear();
                                    return;
                                }
                                if(!text_img.getText().toString().equals("")){
                                    addViewInfoList.setImage(text_img.getText().toString());
                                }
                                if(!number.getText().toString().equals("")){
                                    addViewInfoList.setNum(number.getText().toString());
                                }
                                addViewInfoList.setType("1");
                                mlsit.add(addViewInfoList);
                            }
                        }
                    } else if (rbDb.isChecked()) {
                        if(!edit_dbr_id.getText().toString().equals("")){
                            AddViewInfoBean.Guarantee guarantee = addViewInfoBean.getGuaranteeList();
                            guarantee.setType("2");
                            guarantee.setGuarantee(edit_dbr_id.getText().toString());
                            mlsit.add(guarantee);
                        }
                    }
//                }
            }
        }

        if (mlsit.size() != 0) {
            Gson gson = new Gson();
            confirm = gson.toJson(mlsit);
            Log.e("TAG", "contractShowadd   " + confirm);
            Showadd();
        } else {
            confirm = "";
            Log.e("TAG", "contractShowadds   " + confirm);
            Showadds();
        }


    }

    //无确认方式
    private void Showadds() {
        String sched_id = SPUtils.get(ReserveActivity.this, "sched_id", "").toString();
        Gson gsonS = new Gson();
        banquet_name = gsonS.toJson(BanquetNamelsit);
        String string = editCb.getText().toString();
        OkHttpUtils.post()
                .url(Api.POST_SCHEDUIED)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("bid", sched_id)
                .addParams("tid", tid)
                .addParams("begin_year", begin_year)
                .addParams("begin_month", begin_month)
                .addParams("begin_day", begin_day)
                .addParams("begin_night", begin_night)
                .addParams("end_year", end_year)
                .addParams("end_month", end_month)
                .addParams("end_day", end_day)
                .addParams("end_night", end_night)
                .addParams("book", editTx.getText().toString())
                .addParams("mobile", textPhoto.getText().toString())
                .addParams("sex", sex)
                .addParams("desk", textDesk.getText().toString())
                .addParams("prepare_desk", prepareDesk.getText().toString())
                .addParams("did", ""+cb_id)//cb_id
                .addParams("d_money", d_money)
                .addParams("d_name", string)//餐标名称
                .addParams("cid", cb_did)
                .addParams("source", source_id)
                .addParams("content", editContent.getText().toString())
                .addParams("contract", contract)
                .addParams("other", other)
                .addParams("is_meeting", is_meeting)
                .addParams("increase", increase)
                .addParams("banquet_name", banquet_name)
                .addParams("director_id", director_id)
                .addParams("type", type + "")
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "预定下单onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "预定下单onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("0")) {
                            ToastUtils.show(ReserveActivity.this, beanBean.getMsg()+"");
                            SPUtils.put(ReserveActivity.this, "aBoolean", "1");
                            EventBean eventBusBean=new EventBean();
                            eventBusBean.setAnInt(0);
                            EventBus.getDefault().post(eventBusBean); //普通事件发布
                            Intent intent = new Intent(mContext, MainActivity.class).
                            setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            finish();
                        } else if (beanBean.getCode().equals("4")) {
                            SPUtils.put(mContext, "token", "");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        } else {
                            ToastUtils.show(ReserveActivity.this, beanBean.getMsg());
                        }

                    }
                });
    }

    //有确认方式
    private void Showadd() {
        Gson gsonS = new Gson();
        banquet_name = gsonS.toJson(BanquetNamelsit);
        String sched_id = SPUtils.get(ReserveActivity.this, "sched_id", "").toString();
        String string = editCb.getText().toString();
        OkHttpUtils.post()
                .url(Api.POST_SCHEDUIED)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("bid", sched_id)
                .addParams("tid", tid)
                .addParams("begin_year", begin_year)
                .addParams("begin_month", begin_month)
                .addParams("begin_day", begin_day)
                .addParams("begin_night", begin_night)
                .addParams("end_year", end_year)
                .addParams("end_month", end_month)
                .addParams("end_day", end_day)
                .addParams("end_night", end_night)
                .addParams("book", editTx.getText().toString())
                .addParams("mobile", textPhoto.getText().toString())
                .addParams("sex", sex)
                .addParams("desk", textDesk.getText().toString())
                .addParams("prepare_desk", prepareDesk.getText().toString())
                .addParams("did", ""+cb_id)//cb_id
                .addParams("d_money", d_money)
                .addParams("d_name", string)//餐标名称
                .addParams("cid", cb_did)
                .addParams("source", source_id)
                .addParams("content", editContent.getText().toString())
                .addParams("contract", contract)
                .addParams("other", other)
                .addParams("is_meeting", is_meeting)
                .addParams("increase", increase)
                .addParams("banquet_name", banquet_name)
                .addParams("confirm", confirm)
                .addParams("director_id", director_id)
                .addParams("type", type + "")
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "预定下单onError   " + e.getMessage());
                        new Handler(Looper.getMainLooper()).post(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtils.show(ReserveActivity.this, "" + e.getMessage());
                            }
                        });
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "预定下单onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("0")) {
                            ToastUtils.show(ReserveActivity.this, ""+beanBean.getMsg());
                            SPUtils.put(ReserveActivity.this, "aBoolean", "1");
                            EventBean eventBusBean=new EventBean();
                            eventBusBean.setAnInt(0);
                            EventBus.getDefault().post(eventBusBean); //普通事件发布
                            Intent intent = new Intent(mContext, MainActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(intent);
                            finish();
                        } else if (beanBean.getCode().equals("4")) {
                            SPUtils.put(mContext, "token", "");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        } else {
                            ToastUtils.show(ReserveActivity.this, beanBean.getMsg());
                        }
                    }
                });
    }

    //确认方式布局
    private void initFind(View childAt, int childCount) {
        text_title = childAt.findViewById(R.id.text_title);
        number = childAt.findViewById(R.id.number);
        imgview = childAt.findViewById(R.id.imgview);
        money = childAt.findViewById(R.id.money);
        text_img = childAt.findViewById(R.id.text_img);
        tv_type = childAt.findViewById(R.id.tv_type);
        tv_type_id = childAt.findViewById(R.id.tv_type_id);
        tv_mode = childAt.findViewById(R.id.tv_mode);
        tv_mode_id = childAt.findViewById(R.id.tv_mode_id);
        rbYs = childAt.findViewById(R.id.rb_ys);
        rbDb = childAt.findViewById(R.id.rb_db);
        lineDbr = childAt.findViewById(R.id.line_dbr);
        edit_dbr = childAt.findViewById(R.id.edit_dbr);
        img_dbr = childAt.findViewById(R.id.img_dbr);
        edit_dbr_id = childAt.findViewById(R.id.edit_dbr_id);
        line_ys = childAt.findViewById(R.id.line_ys);
        bh=childAt.findViewById(R.id.bh);
        rbDb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int img = (int) childAt.getTag();
                for (int i = 0; i < lineAddView.getChildCount(); i++) {
                    View childAt = lineAddView.getChildAt(i);
                    initFind(childAt, i);
                    if (i == img) {
                        rbDb.setChecked(true);
                        rbYs.setChecked(false);
                        lineDbr.setVisibility(View.VISIBLE);
                        line_ys.setVisibility(View.GONE);
                    }
                }
            }
        });
        rbYs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int img = (int) childAt.getTag();
                for (int i = 0; i < lineAddView.getChildCount(); i++) {
                    View childAt = lineAddView.getChildAt(i);
                    initFind(childAt, i);
                    if (i == img) {
                        rbDb.setChecked(false);
                        rbYs.setChecked(true);
                        lineDbr.setVisibility(View.GONE);
                        line_ys.setVisibility(View.VISIBLE);
                    }
                }
            }
        });
        img_dbr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int img = (int) childAt.getTag();
                DataGuaranteeDialog.show(ReserveActivity.this, null, Api.POST_GUARANTEE).setListener(new DataGuaranteeDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String id, String name) {
                        for (int i = 0; i < lineAddView.getChildCount(); i++) {
                            View childAt = lineAddView.getChildAt(i);
                            initFind(childAt, i);
                            if (i == img) {
                                edit_dbr.setText("" + name);
                                edit_dbr_id.setText(id + "");
                            }
                        }
                    }
                });
            }
        });
        edit_dbr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int img = (int) childAt.getTag();
                DataGuaranteeDialog.show(ReserveActivity.this, null, Api.POST_GUARANTEE).setListener(new DataGuaranteeDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String id, String name) {
                        for (int i = 0; i < lineAddView.getChildCount(); i++) {
                            View childAt = lineAddView.getChildAt(i);
                            initFind(childAt, i);
                            if (i == img) {
                                edit_dbr.setText("" + name);
                                edit_dbr_id.setText(id + "");
                            }
                        }

                    }
                });
            }
        });
        tv_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int img = (int) childAt.getTag();
                DataReserveAddViewDialog.show(ReserveActivity.this, null, SpTypeList).setListener(new DataReserveAddViewDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String id, String name) {
                        for (int i = 0; i < lineAddView.getChildCount(); i++) {
                            View childAt = lineAddView.getChildAt(i);
                            initFind(childAt, i);
                            if (i == img) {
                                tv_type.setText("" + name);
                                tv_type_id.setText("" + id);
                            }
                        }
                    }
                });
            }
        });
        tv_mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int img = (int) childAt.getTag();
                DataReserveAddViewDialog.show(ReserveActivity.this, null, SpModeList).setListener(new DataReserveAddViewDialog.OnDialogClickListener() {
                    @Override
                    public void onPositiveClick(String id, String name) {
                        for (int i = 0; i < lineAddView.getChildCount(); i++) {
                            View childAt = lineAddView.getChildAt(i);
                            initFind(childAt, i);
                            if (i == img) {
                                tv_mode.setText("" + name);
                                tv_mode_id.setText("" + id);
                            }
                        }
                    }
                });
            }
        });
        childCount = childCount + 1;
        String d = DatePhotoUtils.numberToChinese(childCount);
        if (d.equals("一")) {
            text_title.setText("   首次金额");
        } else {
            text_title.setText("   " + d + "次金额");
        }

        imgview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (!text_img.getText().toString().equals("")) {
//                    ImageDialog.show(ReserveActivity.this, null, text_img.getText().toString());
//                    return;
//                }
                img = (int) childAt.getTag();
                PictureSelector.create(ReserveActivity.this)
                        .openGallery(PictureMimeType.ofImage())
                        .loadImageEngine(GlideEngine.createGlideEngine())
                        .selectionMode(100)
                        .maxSelectNum(1)
                        .previewImage(true)
                        .compress(true)//图片是否压缩
                        .minimumCompressSize(70)// 小于100kb的图片不压缩
                        .forResult(123);
            }
        });

    }

    // 普通事件的处理
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEvent(EventBusResereBean carrier) {
        if (carrier != null) {
            if (carrier.getType() == 1) {
                if (type_time == 1) {
                    if(is_meeting.equals("1")){
                        long stringToDate1 = getStringToDate(carrier.getStarttime());
                        long stringToDate = getStringToDate(textTime2.getText().toString().replace("【日期】",""));
                        if(stringToDate1>stringToDate){
                            ToastUtils.shortToast(ReserveActivity.this,"开始时间不能大于结束时间");
                            return;
                        }
                    }
                    String[] two = carrier.getStarttime().split("-");
                    begin_year = two[0];
                    begin_month = two[1];
                    begin_day = two[2];
                    textTime1.setText("【日期】" + carrier.getStarttime());
                    time=begin_year+"-"+begin_month+"-"+begin_day;
                } else {
                    if(is_meeting.equals("1")) {
                        long stringToDate1 = getStringToDate(carrier.getStarttime());
                        long stringToDate = getStringToDate(textTime1.getText().toString().replace("【日期】", ""));
                        if (stringToDate1 < stringToDate) {
                            ToastUtils.shortToast(ReserveActivity.this, "结束时间不能小于开始时间");
                            return;
                        }
                    }
                    String[] two = carrier.getStarttime().split("-");
                    end_year = two[0];
                    end_month = two[1];
                    end_day = two[2];
                    textTime2.setText(carrier.getStarttime());
                }
            } else if (carrier.getType() == 4) {
                if(!carrier.getMoney().equals("")){
                    increase_money = carrier.getMoney();
                    increase = carrier.getId();
                    textService.setText("已添加");
                    textService.setTextColor(getResources().getColor(R.color.color_4d89ea));
                    textService.setTextSize(15);
                }else{
                    increase_money = "";
                    increase = "";
                    textService.setText("+");
                    textService.setTextColor(getResources().getColor(R.color.color_333333));
                    textService.setTextSize(20);
                }

            } else if (carrier.getType() == 5) {
                if(!carrier.getId().equals("")){
                    if(!cb_id.equals("")){
                        cb_id = carrier.getId();
                    }
                }
                cb_did = carrier.getDid();
                textCaidan.setText("已添加");
                textCaidan.setTextColor(getResources().getColor(R.color.color_4d89ea));
                textCaidan.setTextSize(15);
            }
        }
    }

    public static long getStringToDate(String dateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try{
            date = dateFormat.parse(dateString);
        } catch(ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this); //解除注册
    }
    //客户经理 客户来源  担保人
    private void showData() {
        String string = SPUtils.get(ReserveActivity.this, "id", "").toString();
        OkHttpUtils.post()
                .url(Api.POST_ZERO_VIEW)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "客户经理onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "客户经理onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(mContext, "token", "");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        ZeroSpSourceBean zeroSpSourceBean = JsonUtils.parseByGson(response, ZeroSpSourceBean.class);
                        if (beanBean.getCode().equals("0")) {
                            list = zeroSpSourceBean.getData().getList();
                            if (list.size() != 0) {
                                for (int i = 0; i < list.size(); i++) {
                                    if (string.equals("" + list.get(i).getId())) {
                                        director_id = list.get(i).getId() + "";
                                        tvView.setText("" + list.get(i).getUsername());
                                        break;
                                    }
                                }

                            }
                        }
                    }
                });
        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_SOURCE)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "客户来源onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "客户来源onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(mContext, "token", "");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        ZeroSpSourceBean zeroSpSourceBean = JsonUtils.parseByGson(response, ZeroSpSourceBean.class);
                        if (beanBean.getCode().equals("0")) {
                            list = zeroSpSourceBean.getData().getList();
                            if (list.size() != 0) {
                                source_id = list.get(0).getId() + "";
                                tvSource.setText("" + list.get(0).getName());
                            }
                        }
                    }
                });

        OkHttpUtils.post()
                .url(Api.POST_GUARANTEE)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "客户来源onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "客户来源onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(mContext, "token", "");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        GuaranteeInfoBean guaranteeInfoBean = JsonUtils.parseByGson(response, GuaranteeInfoBean.class);
                        if (beanBean.getCode().equals("0")) {
                            if (guaranteeInfoBean.getData().getGuarantee().size() == 0) {
                                return;
                            }
                            editdbrid = guaranteeInfoBean.getData().getGuarantee().get(0).getId() + "";
                            editdbr = guaranteeInfoBean.getData().getGuarantee().get(0).getName();
                        }
                    }
                });

        OkHttpUtils.post()
                .url(Api.POST_SCHEDULED_STANDARD)
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString())
                .addParams("bid", officeid)
                .addParams("tid",tid)
                .tag(this)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.e("TAG", "餐标onError   " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Log.e("TAG", "餐标onResponse   " + response);
                        BeanBean beanBean = JsonUtils.parseByGson(response, BeanBean.class);
                        if (beanBean.getCode().equals("4")) {
                            SPUtils.put(mContext, "token", "");
                            Intent intent = new Intent(mContext, LoginActivity.class).
                                    setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                            return;
                        }
                        DialogDataReserveCbBean scheduledBean = JsonUtils.parseByGson(response, DialogDataReserveCbBean.class);
                        if(beanBean.getCode().equals("0")){
                            if (scheduledBean.getData().getData().size() != 0) {
                                List<DialogDataReserveCbBean.DataBeanX.DataBean> data = scheduledBean.getData().getData();
                                for (int i = 0; i < data.size(); i++) {
                                    if (Float.valueOf(string) > Float.valueOf(data.get(i).getMoney())) {
                                        cb_id = data.get(i).getId() + "";
                                        cb_did = data.get(i).getDid();
                                        d_money = data.get(i).getMoney();
                                        editCb.setText("" + data.get(i).getName());
                                        textCaidan.setText("已添加");
                                        textCaidan.setTextColor(getResources().getColor(R.color.color_4d89ea));
                                        textCaidan.setTextSize(15);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                if (data == null) {
                    return;
                }
                Uri uri = data.getData();
                String[] contacts = DatePhotoUtils.getPhoneContacts(ReserveActivity.this, uri);
                editTx.setText("" + contacts[0]); //
                textPhoto.setText("" + contacts[1].trim().replace("-", "").replace(" ", ""));
                break;
            case 188:
                if (data == null) {
                    return;
                }
                List<LocalMedia> localMedia = PictureSelector.obtainMultipleResult(data);
                if (localMedia != null) {
                    if (img_type == 2) {
                        if(mlist_qita.size()!=0){
                            if(mlist_qita.get(0).equals("")){
                                mlist_qita.remove(0);
                            }
                        }
                        for (int i = 0; i < localMedia.size(); i++) {
                            mlist_qita.add(localMedia.get(i).getCompressPath());
                        }
                        showRVQTImg();
                    } else if (img_type == 1) {
                        if(mlist_hetong.size()!=0){
                            if(mlist_hetong.get(0).equals("")){
                                mlist_hetong.remove(0);
                            }
                        }
                        for (int i = 0; i < localMedia.size(); i++) {
                            mlist_hetong.add(localMedia.get(i).getCompressPath());
                        }
                        showRVHtImg();
                    }
                }
                break;
            case 123:
                if (data == null) {
                    return;
                }
                final String[] imgString = new String[1];
                List<LocalMedia> localMedias = PictureSelector.obtainMultipleResult(data);
                String realPath;
                if (localMedias.get(0).getRealPath() != null) {
                    realPath = localMedias.get(0).getRealPath();
                } else {
                    realPath = localMedias.get(0).getCompressPath();
                }
                PostFormBuilder builder = OkHttpUtils.post()
                        .url(Api.POSY_UPLOADS);
                File file = new File(realPath);
                builder.addFile("file[]", file.getName(), file);
                showLoadingDialog();
                builder.build().execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        hideLoadingDialog();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        hideLoadingDialog();
                        Log.e("TAG", "其他图片onResponse~~~~~~~~    " + response);
                        ImgBean imgBean = JsonUtils.parseByGson(response, ImgBean.class);
                        if (imgBean.getCode().equals("0")) {
                            imgString[0] = imgBean.getData().get(0).getUrl();
                            AddViewImg addViewImg = new AddViewImg();
                            addViewImg.setImg_type(img);
                            addViewImg.setImgurl(imgString[0]);
                            addViewImg.setId(imgBean.getData().get(0).getId());
                            addViewImgList.add(addViewImg);
                        }
                        for (int i = 0; i < lineAddView.getChildCount(); i++) {
                            View childAt = lineAddView.getChildAt(i);
                            initFind(childAt, i);
                            for (int j = 0; j < addViewImgList.size(); j++) {
                                if (i == addViewImgList.get(j).getImg_type()) {
//                                    text_img.setText("" + addViewImgList.get(j).getImgurl());
                                    text_img.setText("" + addViewImgList.get(j).getId());
                                    Glide.with(ReserveActivity.this).load(imgString[0]).into(imgview);
                                }
                            }

                        }
                    }
                });

                break;

        }
    }

    public void showFileImg() {
        if(mlist_qita.size()!=0){
            if(mlist_qita.get(0).equals("")){
                mlist_qita.remove(0);
            }
        }
        if (mlist_qita.size() != 0) {
            initUploadQT();
        } else {
            if(mlist_hetong.size()!=0){
                if(mlist_hetong.get(0).equals("")){
                    mlist_hetong.remove(0);
                }
            }
            if (mlist_hetong.size() != 0) {
                mlist_qita.add("");
                initUploadHT();
            } else {
                mlist_qita.add("");
                mlist_hetong.add("");
                showDataAddReserve();
            }
        }
    }

    //其他
    private void initUploadQT() {
        PostFormBuilder builder = OkHttpUtils.post()
                .url(Api.POSY_UPLOADS)
                .addHeader("Content-Type", "application/json; charset=utf-8")
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString());
        for (int i = 0; i < mlist_qita.size(); i++) {
            String path = mlist_qita.get(i);
            if (path != null && !path.isEmpty()) {
                File file = new File(path);
                builder.addFile("file[]", file.getName(), file);
            }
        }
        showLoadingDialog();
        builder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("TAG", "onError: "+e.getMessage() );
                hideLoadingDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                Log.e("TAG", "其他图片onResponse~~~~~~~~    " + response);
                ImgBean imgBean = JsonUtils.parseByGson(response, ImgBean.class);
                StringBuffer sb = new StringBuffer();
                if (imgBean.getCode().equals("0")) {
                    for (int i = 0; i < imgBean.getData().size(); i++) {
                        ImgBean.DataBean dataBean = imgBean.getData().get(i);
                        sb.append(dataBean.getId() + ",");
                    }
                    if (!sb.toString().equals("")) {
                        other = sb.toString().substring(0, sb.toString().length() - 1);
                    }

                }
                initUploadHT();
            }
        });
    }

    //合同
    private void initUploadHT() {
        PostFormBuilder builder = OkHttpUtils.post()
                .url(Api.POSY_UPLOADS)
                .addHeader("Content-Type", "application/json; charset=utf-8")
                .addHeader("flag", SPUtils.get(mContext, "token", "").toString());
        for (int i = 0; i < mlist_hetong.size(); i++) {
            String path = mlist_hetong.get(i);
            if (path != null && !path.isEmpty()) {
                File file = new File(path);
                builder.addFile("file[]", file.getName(), file);
            }
        }
        showLoadingDialog();
        builder.build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.e("TAG", "onError: "+e.getMessage() );
                hideLoadingDialog();
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                Log.e("TAG", "合同图片onResponse~~~~~~~~    " + response);
                ImgBean imgBean = JsonUtils.parseByGson(response, ImgBean.class);
                StringBuffer sb = new StringBuffer();
                if (imgBean.getCode().equals("0")) {
                    for (int i = 0; i < imgBean.getData().size(); i++) {
                        ImgBean.DataBean dataBean = imgBean.getData().get(i);
                        sb.append(dataBean.getId() + ",");
                    }
                    if (!sb.toString().equals("")) {
                        contract = sb.toString().substring(0, sb.toString().length() - 1);
                    }
                }
                showDataAddReserve();
            }
        });
    }

    //预收款 图片
    public class AddViewImg {
        private String imgurl;
        private int img_type;
        private String id;

        public String getImgurl() {
            return imgurl;
        }

        public void setImgurl(String imgurl) {
            this.imgurl = imgurl;
        }

        public int getImg_type() {
            return img_type;
        }

        public void setImg_type(int img_type) {
            this.img_type = img_type;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }
    }

    //新娘新郎
    public class BanquetNameBean {
        private String bid;
        private String name;
        private String value;

        public String getBid() {
            return bid;
        }

        public void setBid(String bid) {
            this.bid = bid;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    public class SolveEditTextScrollClash implements View.OnTouchListener {

        private EditText editText;

        public SolveEditTextScrollClash(EditText editText) {
            this.editText = editText;
        }

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            //触摸的是EditText而且当前EditText能够滚动则将事件交给EditText处理。否则将事件交由其父类处理
            if ((view.getId() == editText.getId() && canVerticalScroll(editText))) {
                view.getParent().requestDisallowInterceptTouchEvent(true);
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    view.getParent().requestDisallowInterceptTouchEvent(false);
                }
            }
            return false;
        }

        /**
         * EditText竖直方向能否够滚动
         *
         * @param editText 须要推断的EditText
         * @return true：能够滚动   false：不能够滚动
         */
        private boolean canVerticalScroll(EditText editText) {
            //滚动的距离
            int scrollY = editText.getScrollY();
            //控件内容的总高度
            int scrollRange = editText.getLayout().getHeight();
            //控件实际显示的高度
            int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() - editText.getCompoundPaddingBottom();
            //控件内容总高度与实际显示高度的差值
            int scrollDifference = scrollRange - scrollExtent;

            if (scrollDifference == 0) {
                return false;
            }

            return (scrollY > 0) || (scrollY < scrollDifference - 1);
        }
    }

    protected void judgePermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 检查该权限是否已经获取
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝
            //手机状态权限
            String[] SdCdPermission = {Manifest.permission.READ_CONTACTS};
            if (ContextCompat.checkSelfPermission(this, SdCdPermission[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, SdCdPermission, 350);
            }else{
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, 0);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NotNull @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean hasAllGranted = true;
        //判断是否拒绝  拒绝后要怎么处理 以及取消再次提示的处理
        for (int grantResult : grantResults) {
            if (grantResult == PackageManager.PERMISSION_DENIED) {
                hasAllGranted = false;
                break;
            }
        }
        if (hasAllGranted) { //同意权限做的处理,开启服务提交通讯录
            Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, 0);

        } else {    //拒绝授权做的处理，弹出弹框提示用户授权
            dealwithPermiss(ReserveActivity.this, permissions[0]);
        }
    }

    public void dealwithPermiss(final Activity context, String permission) {
        if (!ActivityCompat.shouldShowRequestPermissionRationale(context, permission)) {
            AlertDialog.Builder builder = new AlertDialog.Builder(ReserveActivity.this);
            builder.setTitle("操作提示")
                    .setMessage("注意：当前缺少必要权限！\n请点击“设置”-“权限”-打开所需权限\n最后点击两次后退按钮，即可返回")
                    .setPositiveButton("去授权", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                            Uri uri = Uri.fromParts("package", context.getApplicationContext().getPackageName(), null);
                            intent.setData(uri);
                            context.startActivity(intent);
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(ReserveActivity.this, "取消授权", Toast.LENGTH_SHORT).show();
                        }
                    }).show();

        }
    }
}